package com.yanqu.road.server.cmd.activity.pawnshop;

import java.util.Map;

import com.yanqu.road.entity.activity.pawnshop.*;
import com.yanqu.road.server.pb.PawnshopPb;
import com.yanqu.road.pb.activity.PawnshopProto;
import com.yanqu.road.pb.activity.PawnshopProto.DPawnshopStaff;
import com.yanqu.road.pb.activity.PawnshopProto.PawnshopConfigRespMsg;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.pawnshop.PawnshopMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_PAWNSHOP_CONFIG, desc = "请求当铺配置")
public class PawnshopConfigCmd extends AbstractCommand {

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if (!PawnshopMgr.isActivityInTime()) {
			// 非活动时间内
			this.ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
	}

	@Override
	public void sendData(GamePlayer player) {
		PawnshopConfigRespMsg.Builder respMsg = PawnshopConfigRespMsg.newBuilder();
		respMsg.setRet(ret);
		if (ret == 0) {
		    //当铺配置
            for(ConfPawnshop confPawnshop : PawnshopMgr.config.getConfPawnshopMap().values()){
                PawnshopProto.DPawnshopTempMsg.Builder pawnshopMsg = PawnshopPb.parseDPawnshopTempMsg(confPawnshop);
                if(null != pawnshopMsg) {
                    respMsg.addDPawnshopList(pawnshopMsg);
                }
            }
			// 员工配置
			Map<Integer, Map<Integer, ConfPawnshopStaff>> tMap = PawnshopMgr.config.getConfPawnshopStaffMap();
			for (Map<Integer, ConfPawnshopStaff> staffMap : tMap.values()) {
				for (ConfPawnshopStaff confStaff : staffMap.values()) {
					DPawnshopStaff.Builder staffMsg = PawnshopPb.parseDPawnshopStaff(confStaff);
					if(null != staffMsg) {
						respMsg.addDStaffList(staffMsg);
					}
				}
			}
			//物件配置
			for (ConfPawnshopItem confPawnshopItem : PawnshopMgr.config.getConfPawnshopItemMap().values()){
				PawnshopProto.DPawnshopItemTempMsg.Builder itemMsg = PawnshopPb.parseDPawnshopItemTempMsg(confPawnshopItem);
				if(null != itemMsg){
					respMsg.addDItemList(itemMsg);
				}
			}
			//物件等级配置
			for (ConfPawnshopItemLv confPawnshopItemLv : PawnshopMgr.config.getConfPawnshopItemLvMap().values()){
				PawnshopProto.DPawnshopItemLevelTempMsg.Builder lvMsg = PawnshopPb.parseDPawnshopItemLevelTempMsg(confPawnshopItemLv);
				if(null != lvMsg){
					respMsg.addDItemLvList(lvMsg);
				}
			}

			//时事事件配置
			for(ConfPawnshopEvent confPawnshopEvent : PawnshopMgr.config.getConfPawnshopEventMap().values()){
				PawnshopProto.DPawnshopPoliticsEventTempMsg.Builder eventMsg = PawnshopPb.parseDPawnshopPoliticsEventTempMsg(confPawnshopEvent);
				if(null != eventMsg){
					respMsg.addEventList(eventMsg);
				}
			}
			// 其他配置
			respMsg.setEventBuyFakeBuyRate(PawnshopMgr.config.eventBuyFakeBuyRate);
			respMsg.setEventSellFakeBuyRate(PawnshopMgr.config.eventSellFakeBuyRate);
			respMsg.setEventHockBuyRate(PawnshopMgr.config.eventHockBuyRate);
		}
		// 返回当铺配置
		player.sendPacket(ClientProtocol.U_PAWNSHOP_CONFIG, respMsg);
	}
}
